package com.dongdongshop.service.impl;

import com.dongdongshop.data.PageResult;
import com.dongdongshop.data.PageUtil;
import com.dongdongshop.domain.model.TbSpecification;
import com.dongdongshop.domain.model.TbSpecificationExample;
import com.dongdongshop.domain.model.TbSpecificationOption;
import com.dongdongshop.domain.model.TbSpecificationOptionExample;
import com.dongdongshop.domain.vo.SpecificationOptionVO;
import com.dongdongshop.domain.vo.SpecificationVO;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.service.SpecificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.alibaba.cloud.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private TbSpecificationMapper specificationMapper;

    @Autowired
    private TbSpecificationOptionMapper optionMapper;

    @Override
    public PageUtil<SpecificationVO> specList(PageResult<SpecificationVO> parms) {
        SpecificationVO parm = parms.getParm();
        TbSpecificationExample specificationExample = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = specificationExample.createCriteria();
        //判断specname是否为空
        if (StringUtils.isNotBlank(parm.getSpecName())){
            criteria.andSpecNameLike("%"+parms.getParm().getSpecName()+"%");
        }
        PageHelper.startPage(parms.getPageNum(), parms.getPageSize());
        List<TbSpecification> tbSpecificationList = specificationMapper.selectByExample(specificationExample);
        PageInfo<TbSpecification> info = new PageInfo<>(tbSpecificationList);
        List<SpecificationVO> specificationVOList = tbSpecificationList.stream().map(spec -> {
            SpecificationVO vo = new SpecificationVO();
            BeanUtils.copyProperties(spec,vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageUtil<>(specificationVOList,info.getTotal(),info.getPageNum(), info.getPageSize());
    }

    @Override
    @Transactional
    public void addSpec(SpecificationVO vo) {
        TbSpecification tbSpecification = new TbSpecification();
        BeanUtils.copyProperties(vo,tbSpecification);
        //增加Specification并返回id
        specificationMapper.addSpecification(tbSpecification);

        //增加SpecificationOption
        List<SpecificationOptionVO> optionVOList = vo.getSpecOptionVoList();
        List<TbSpecificationOption> optionList = optionVOList.stream().map(specOption -> {
            TbSpecificationOption option = new TbSpecificationOption();
            BeanUtils.copyProperties(specOption,option);
            //将获取到的规格id给规格选项
            option.setSpecId(tbSpecification.getId());
            return option;
        }).collect(Collectors.toList());
        //批量增加SpecificationOption
        optionMapper.addManySpecOption(optionList);
    }

    @Override
    @Transactional
    public SpecificationVO selectById(Long id) {
        //查找Specification表
        TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);
        SpecificationVO specificationVO = new SpecificationVO();
        //将查找出的数据存入SpecificationVO
        BeanUtils.copyProperties(tbSpecification,specificationVO);
        //查找SpecificationOption表
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(id);
        List<TbSpecificationOption> optionList = optionMapper.selectByExample(example);
        //将查找出的数据转成SpecificationVO集合
        List<SpecificationOptionVO> voList = optionList.stream().map(option -> {
            SpecificationOptionVO vo = new SpecificationOptionVO();
            BeanUtils.copyProperties(option,vo);
            return vo;
        }).collect(Collectors.toList());
        //将SpecificationVO集合存入SpecificationVO
        specificationVO.setSpecOptionVoList(voList);
        return specificationVO;
    }

    @Override
    @Transactional
    public void updateSpec(SpecificationVO vo) {
        TbSpecification tbSpecification = new TbSpecification();
        BeanUtils.copyProperties(vo,tbSpecification);
        //直接修改Specification表
        specificationMapper.updateByPrimaryKeySelective(tbSpecification);

        //修改SpecificationOption
        //先删除
        TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
        optionExample.createCriteria().andSpecIdEqualTo(tbSpecification.getId());
        optionMapper.deleteByExample(optionExample);
        //再增加
        List<SpecificationOptionVO> optionVOList = vo.getSpecOptionVoList();
        List<TbSpecificationOption> optionList = optionVOList.stream().map(specOption -> {
            TbSpecificationOption option = new TbSpecificationOption();
            BeanUtils.copyProperties(specOption,option);
            //规格id给规格选项
            option.setSpecId(tbSpecification.getId());
            return option;
        }).collect(Collectors.toList());
        //增加
        optionMapper.addManySpecOption(optionList);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        //删除Specification表
        specificationMapper.deleteByPrimaryKey(id);
        //删除SpecificationOption表
        TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
        optionExample.createCriteria().andSpecIdEqualTo(id);
        optionMapper.deleteByExample(optionExample);
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //批量删除Specification表
        TbSpecificationExample example = new TbSpecificationExample();
        example.createCriteria().andIdIn(ids);
        specificationMapper.deleteByExample(example);
        //批量删除SpecificationOption表
        optionMapper.deleteMany(ids);
    }

    @Override
    @Transactional
    public void delThis(Long id) {
        optionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SpecificationVO> getAllSpec() {
        List<TbSpecification> tbSpecificationList = specificationMapper.selectByExample(null);
        List<SpecificationVO> voList = tbSpecificationList.stream().map(tbSpecification -> {
            SpecificationVO vo = new SpecificationVO();
            BeanUtils.copyProperties(tbSpecification, vo);
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }

    @Override
    public List<SpecificationOptionVO> specLOptionist() {
        List<TbSpecificationOption> tbSpecificationList = optionMapper.selectByExample(null);
        return tbSpecificationList.stream().map(tbSpecification -> {
            SpecificationOptionVO vo = new SpecificationOptionVO();
            BeanUtils.copyProperties(tbSpecification, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}
